package com.agregatesdk.core;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;

public class UserAccount {

    private static UserAccount instance = new UserAccount();

    private Context context;
    private String username;
    private String password;
    private String sex;
    private String bornDate;
    private String idcard;
    private String macid;
    private String deviceid;
    private String location;
    private String position;
    private int monthIncome;
    private String bankCard;
    private String bankName;
    private String ip;
    private double restMoney;
    private String uid;
    private String nickname;
    private int user_type;

    private String sinaAccessToken;
    private String qqAccessToken;

    private String sinaExpiresIn;
    private String qqExpiresIn;

    private String picPath;

    private String amount;
    private String rest;

    private int versionCode;
    private int channel;

    public static UserAccount getInstance() {
        return instance;
    }

    private UserAccount() {
    }

    public void init(Context context) {
        this.context = context;

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm == null) {
            return;
        }
        deviceid = tm.getDeviceId();
        WifiManager wifi_service = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
        WifiInfo wifiinfo = wifi_service.getConnectionInfo();
        // WiFi网卡的MAC地址， 如 00:24:a5:34:b7:7e
        if (wifiinfo.getMacAddress() != null) {
            macid = wifiinfo.getMacAddress();
        }
        ip = String.valueOf(wifiinfo.getIpAddress());

        this.channel = getSDKChannel(context);

        try {
            setVersionCode(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static int getSDKChannel(Context paramContext) {
        int localObject1 = 0;
        try {
            PackageManager localPackageManager = paramContext.getPackageManager();
            ApplicationInfo localApplicationInfo = localPackageManager.getApplicationInfo(paramContext.getPackageName(), PackageManager.GET_META_DATA);
            if ((localApplicationInfo != null) && (localApplicationInfo.metaData != null)) {
                Object localObject2 = localApplicationInfo.metaData.get("RECOMMEND_CHANNEL_ID");
                if (localObject2 != null) {
                    String str = localObject2.toString();
                    if (str != null) {
                        localObject1 = Integer.valueOf(str);
                        // else if (ZhuamobTargeting.getRunMode() ==
                        // ZhuamobTargeting.RunMode.TEST)
                        // Log.i(Constant.TAG,
                        // "Could not read MJ_SDK_CHANNEL meta-data from AndroidManifest.xml.");
                    } else {
                        Log.i("zcm", "Could not read MJ_SDK_CHANNEL meta-data from AndroidManifest.xml.");

                    }
                }
            }
        } catch (Throwable e) {
            // 如果是测试模式
            // if (ZhuamobTargeting.getRunMode() ==
            // ZhuamobTargeting.RunMode.TEST) {
            Log.i("zcm", "Could not read MJ_SDK_CHANNEL meta-data from AndroidManifest.xml.");
            // }
        }
        return localObject1;
    }

    /**
     * @return the channel
     */
    public int getChannel() {
        return channel;
    }

    /**
     * @param channel
     *            the channel to set
     */
    public void setChannel(int channel) {
        this.channel = channel;
    }

    /**
     * @return the location
     */
    public String getLocation() {
        return location;
    }

    /**
     * @param location
     *            the location to set
     */
    public void setLocation(String location) {
        this.location = location;
    }

    /**
     * @return the sinaExpiresIn
     */
    public String getSinaExpiresIn() {
        return sinaExpiresIn;
    }

    /**
     * @param sinaExpiresIn
     *            the sinaExpiresIn to set
     */

    /**
     * @return the qqExpiresIn
     */
    public String getQqExpiresIn() {
        return qqExpiresIn;
    }

    /**
     * @param qqExpiresIn
     *            the qqExpiresIn to set
     */

    /**
     * @return the picPath
     */
    public String getPicPath() {
        return picPath;
    }

    /**
     * @param picPath
     *            the picPath to set
     */
    public void setPicPath(String picPath) {
        this.picPath = picPath;
    }

    /**
     * @return the sinaAccessToken
     */
    public String getSinaAccessToken() {
        return sinaAccessToken;
    }

    /**
     * @return the qqAccessToken
     */
    public String getQqAccessToken() {
        return qqAccessToken;
    }

    /**
     * @return the uid
     */
    public String getUid() {

        if (Utils.isNullorEmpty(uid)) {
            uid = "0000000000000000000000000";
        }

        return uid;
    }

    /**
     * @return the nickname
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * @param nickname
     *            the nickname to set
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * @return the user_type
     */
    public int getUser_type() {

        if (user_type != 0 && user_type != 1) {
            user_type = 0;
        }

        return user_type;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username
     *            the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * @param sex
     *            the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * @return the bornDate
     */
    public String getBornDate() {
        return bornDate;
    }

    /**
     * @param bornDate
     *            the bornDate to set
     */
    public void setBornDate(String bornDate) {
        this.bornDate = bornDate;
    }

    /**
     * @return the idcard
     */
    public String getIdcard() {
        return idcard;
    }

    /**
     * @param idcard
     *            the idcard to set
     */
    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    /**
     * @return the macid
     */
    public String getMacid() {

        if (Utils.isNullorEmpty(macid)) {
            macid = "00:00:00:00:00:00";
        }

        return macid;
    }

    /**
     * @param macid
     *            the macid to set
     */
    public void setMacid(String macid) {
        this.macid = macid;
    }

    /**
     * @return the deviceid
     */
    public String getDeviceid() {
        if (deviceid == null || deviceid.trim().equals("")) {
            deviceid = "aaaaaaaaaaaaaaaaaaaa";
        }
        return deviceid;
    }

    /**
     * @param deviceid
     *            the deviceid to set
     */
    public void setDeviceid(String deviceid) {
        this.deviceid = deviceid;
    }

    /**
     * @return the position
     */
    public String getPosition() {
        return position;
    }

    /**
     * @param position
     *            the position to set
     */
    public void setPosition(String position) {
        this.position = position;
    }

    /**
     * @return the monthIncome
     */
    public int getMonthIncome() {
        return monthIncome;
    }

    /**
     * @param monthIncome
     *            the monthIncome to set
     */
    public void setMonthIncome(int monthIncome) {
        this.monthIncome = monthIncome;
    }

    /**
     * @return the bankCard
     */
    public String getBankCard() {
        return bankCard;
    }

    /**
     * @param bankCard
     *            the bankCard to set
     */
    public void setBankCard(String bankCard) {
        this.bankCard = bankCard;
    }

    /**
     * @return the bankName
     */
    public String getBankName() {
        return bankName;
    }

    /**
     * @param bankName
     *            the bankName to set
     */
    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    /**
     * @return the restMoney
     */
    public double getRestMoney() {
        return restMoney;
    }

    /**
     * @param restMoney
     *            the restMoney to set
     */
    public void setRestMoney(double restMoney) {
        this.restMoney = restMoney;
    }

    /**
     * @return the ip
     */
    public String getIp() {

        if (Utils.isNullorEmpty(ip)) {
            ip = "123456789";
        }

        return ip;
    }

    /**
     * @param ip
     *            the ip to set
     */
    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * @return the amount
     */
    public String getAmount() {
        if (amount == null || "".equals(amount)) {
            amount = "0";
        }

        return amount;
    }

    /**
     * @param amount
     *            the amount to set
     */
    public void setAmount(String amount) {
        this.amount = amount;
    }

    /**
     * @return the rest
     */
    public String getRest() {

        if (rest == null || "".equals(rest)) {
            rest = "0";
        }

        float tmp = Float.valueOf(rest).floatValue() * 100;

        return Constant.moneysizeFormat.format(tmp);
    }

    /**
     * @param rest
     *            the rest to set
     */
    public void setRest(String rest) {
        this.rest = rest;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

}
